﻿using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Models;
using MiniFox.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Sys.Core.Models.VO
{
    [JsonObject, Table("modules")]
    public class ModuleVo
    {
        [Display(Name = "模块ID")]
        [Column("moduleId")]
        [JsonProperty("moduleId")]
        public string ModuleID { get; set; }

        [Display(Name = "应用系统", ShortName = "应用系统")]
        [Column("appId")]
        [JsonProperty("appId")]
        public string ApplicationID { get; set; }

        [Display(Name = "模块名", ShortName = "模块名")]
        [Column("moduleName")]
        [JsonProperty("moduleName")]
        public string ModuleName { get; set; }

        [Display(Name = "模块地址(url)", ShortName = "模块地址(url)")]
        [Column("uri")]
        [JsonProperty("uri")]
        public string Uri { get; set; }

        [Display(Name = "公共访问", ShortName = "公共访问")]
        [Column("isPublic")]
        [JsonProperty("isPublic")]
        public bool IsPublic { get; set; }

        [Display(Name = "启用", ShortName = "启用")]
        [Column("enabled")]
        [JsonProperty("enabled")]
        public bool Enabled { get; set; }

        [Display(Name = "模块参数")]
        [Column("queryString")]
        [JsonProperty("queryString")]
        public string QueryString { get; set; }

        [Display(Name = "模块图标", ShortName = "模块图标")]
        [Column("icon")]
        [JsonProperty("icon")]
        public string Icon { get; set; }

        [JsonIgnore]
        public string ApplicationDomain { get; set; }

        [JsonIgnore]
        public string ApplicationPath { get; set; }

        [JsonIgnore]
        public bool RequireSSL { get; set; }

        [JsonIgnore]
        public string PortNumber { get; set; }

        [JsonIgnore]
        public string FullHost
        {
            get
            {
                if (!string.IsNullOrEmpty(this.ApplicationDomain))
                {
                    string host = this.ApplicationDomain.Replace("/", "");
                    string protocal = this.RequireSSL ? "https:" : "http:";
                    string port = (string.IsNullOrEmpty(this.PortNumber) || this.PortNumber.Trim() == "80") ? string.Empty : (":" + PortNumber);
                    return (protocal + "//" + host + port);

                }
                else
                {
                    return string.Empty;
                }
            }
        }

        [Column("fullPath")]
        [JsonProperty("fullPath")]
        public string FullPath
        {
            get
            {
                if (string.IsNullOrEmpty(this.ApplicationPath) || this.ApplicationPath.Trim() == "/" || this.ApplicationPath.Trim() == "~/")
                    return FullHost;
                string appPath = this.ApplicationPath.Trim('/');
                return FullHost + "/" + appPath;
            }
        }

        [Display(Name = "完整路径", ShortName = "完整路径"), Editable(false)]
        [Column("fullUri")]
        [JsonProperty("fullUri")]
        public string FullUri
        {
            get
            {
                string fullUri;
                if (string.IsNullOrWhiteSpace(this.Uri) || this.Uri.Trim() == "/" || this.Uri.Trim() == "~/")
                {
                    fullUri = this.FullPath;
                }
                else
                {
                    string uri = this.Uri.Trim('/');
                    if (this.Uri.StartsWith("http://") || this.Uri.StartsWith("https://") || this.Uri.StartsWith("ftp://") || this.Uri.StartsWith("ftps://"))
                        fullUri = uri;
                    else
                        fullUri = this.FullPath + "/" + uri;
                }

                if (!string.IsNullOrWhiteSpace(this.QueryString))
                {
                    fullUri = fullUri.AddQueryString(this.QueryString);
                }
                return fullUri;
            }
        }

        [Display(Name = "打开方式")]
        [Column("openMode")]
        [JsonProperty("openMode")]
        public OpenMode OpenMode { get; set; }

        [Display(Name = "创建时间", ShortName = "创建时间")]
        [Column("createTime")]
        [JsonProperty("createTime")]
        public DateTime CreateTime { get; set; }

        [Display(Name = "上次修改时间", ShortName = "上次修改时间")]
        [Column("updateTime")]
        [JsonProperty("updateTime")]
        public DateTime UpdateTime { get; set; }

        [JsonProperty("actions")]
        public IEnumerable<ModuleActionVo> Actions
        {
            get;set;
        }
    }
}
